package cn.com.ddapp.ddm.customview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/6/16 0016.
 */
public class MySearchFlowLayout extends ViewGroup {
    // 存放所有行的view的集合
    private List<List<View>> mAllView = new ArrayList<List<View>>();
    // 记录每一行的最大高度
    private List<Integer> mLineHeight = new ArrayList<Integer>();

    public MySearchFlowLayout(Context context, AttributeSet attrs) {
        super(context, attrs);

    }

    @Override
    // 以下三个方法 使用系统默认的maginlayoutparams
    protected LayoutParams generateLayoutParams(LayoutParams p) {
        // TODO Auto-generated method stub
        return new MarginLayoutParams(p);
    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        // TODO Auto-generated method stub
        return new MarginLayoutParams(getContext(), attrs);
    }

    @Override
    protected LayoutParams generateDefaultLayoutParams() {
        // TODO Auto-generated method stub
        return new MarginLayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);
    }

    @Override
    // 针对wrap-content的高度：累加totalHeight
    // 负责设置子控件的测量模式和大小 根据所有子控件设置自己的宽和高
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        // 当“wrap-content”高度，要累加总高度
        int totalHeight = 0;
        // 当前行中控件的最高高度
        int lineHeight = 0;
        // 当前行的行宽的总和（类加数）
        int lineWidth = 0;
        int childCount = getChildCount();
        // 遍历每个子控件
        for (int i = 0; i < childCount; i++) {
            View childView = getChildAt(i);
            // 获取每个子控件的宽高
            measureChild(childView, widthMeasureSpec, heightMeasureSpec);
            // 得到每个子控件的布局参数
            MarginLayoutParams lp = (MarginLayoutParams) childView
                    .getLayoutParams();
            // 计算当前子控件的宽高 每一个item的宽高
            int childWidth = childView.getMeasuredWidth() + lp.leftMargin
                    + lp.rightMargin;
            int childHeight = childView.getMeasuredHeight() + lp.topMargin
                    + lp.bottomMargin;
            if (lineWidth + childWidth > widthSize) {// 换行
                lineWidth = childWidth;
                // 累加当前高度
                totalHeight += lineHeight;
                lineHeight = childHeight;
            } else {
                // 累加lineWidht ,lineHeight取最大值
                lineWidth += childWidth;
                lineHeight = Math.max(lineHeight, childHeight);
            }
            // 最后一行时
            if (i == childCount - 1) {
                totalHeight += lineHeight;
            }
            // 高度三种情况:绝对值 match-parent wrap-content
            setMeasuredDimension(widthSize,
                    (heightMode == MeasureSpec.EXACTLY) ? heightSize
                            : totalHeight);
        }
    }

    //
    @Override
    // 调用多次
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        // 每次进来先清除
        mAllView.clear();
        mLineHeight.clear();
        // 获取当前view的宽度
        int layoutWidth = getWidth();
        // 定义每行的宽高
        int lineWidth = 0;
        int lineHeight = 0;
        // 存放每一行所有的childview的集合
        List<View> lineView = new ArrayList<View>();
        int childCount = getChildCount();
        // 遍历操作，把所有的子view和每行的最高高度的数据保存在两集合中
        for (int i = 0; i < childCount; i++) {
            View childView = getChildAt(i);
            MarginLayoutParams lp = (MarginLayoutParams) childView
                    .getLayoutParams();
            // 计算当前子控件的宽高，每一个item的宽高
            int childWidth = childView.getMeasuredWidth() + lp.leftMargin
                    + lp.rightMargin;
            int childHeight = childView.getMeasuredHeight() + lp.topMargin
                    + lp.bottomMargin;
            if (lineWidth + childWidth > layoutWidth) {// 是否换行
                mLineHeight.add(lineHeight);
                mAllView.add(lineView);
                lineWidth = 0;
                lineView = new ArrayList<View>();
            }
            // 如果不需要换行
            lineWidth += childWidth;
            lineHeight = Math.max(lineHeight, childHeight);
            lineView.add(childView);
        }
        // 记录最后一行
        mLineHeight.add(lineHeight);
        mAllView.add(lineView);
        // 在摆放之前，先定义左侧和顶部的累加数
        int mLeft = 0;
        int mTop = 0;
        // 摆放子控件
        // 得到总行数
        int lineNum = mAllView.size();
        for (int i = 0; i < lineNum; i++) {
            // 先得到每一行的所有的view
            lineView = mAllView.get(i);
            // 当前行的最大高度
            lineHeight = mLineHeight.get(i);
            // 遍历当前行的所有的子控件
            for (int j = 0; j < lineView.size(); j++) {
                View childView = lineView.get(j);
                if (childView.getVisibility() == View.GONE) {
                    continue;
                }
                MarginLayoutParams lp = (MarginLayoutParams) childView
                        .getLayoutParams();
                // 计算childview的left/top/right/botton
                int left_child = mLeft + lp.leftMargin;
                int top_child = mTop + lp.topMargin;
                int right_child = left_child + childView.getMeasuredWidth();
                int botton_child = top_child + childView.getMeasuredHeight();
                childView.layout(left_child, top_child, right_child,
                        botton_child);
                mLeft += childView.getMeasuredWidth() + lp.rightMargin
                        + lp.leftMargin;

            }
            mLeft = 0;
            mTop += lineHeight;
        }
    }
}
